
class BookViewModel:
    def __init__(self, book):
        self.title = book['title']
        self.isbn = book['isbn']
        self.publisher = book.get('publisher', "")
        self.pages = book.get('pages', "")
        self.author = book['author']
        self.price = book['price']
        self.summary = book.get('summary', "")
        self.image = book.get('pic', "")
        self.pubdate = book.get('pubdate', "")
        self.binding = book.get('binding', "")
    @property
    def intro(self):
        intros = filter(lambda x: True if x else False,
                        [self.author, self.publisher, self.price])
        return '/'.join(intros)
class BookCollection:
    def __init__(self):
        self.total = 0
        self.books = []
        self.keyword = ''

    def fill(self, yushu_book, keyword):
        self.total = yushu_book.total
        self.keyword = keyword
        self.books = [ BookViewModel(book) for book in yushu_book.books ]
class _BookViewModel:
    # 单个查询结果格式整理
    @classmethod
    def package_single(cls, data, keyword):
        returned = {
            'books': [],
            'total': 0,
            'keyword': keyword
        }
        if data:
            returned['total'] = 1
            returned['books'] = [cls.__cut_book_data(data)]
        return returned

    # 多个查询结果格式整理
    @classmethod
    def package_collection(cls, data,keyword):
        returned = {
            'books': [],
            'total': 0,
            'keyword': keyword
        }
        if data:
            returned['total'] = data['total']
            returned['books'] = [cls.__cut_book_data(book) for book in data['list']]
        return returned

    @classmethod
    def __cut_book_data(cls, data):
        book = {
            'title': data['title'],
            'isbn': data['isbn'],
            'publisher': data.get('publisher', ""),
            'pages': data.get('pages', ""),
            'author': data['author'],
            'price': data['price'],
            'summary': data.get('summary', ''),
            'image': data.get('pic', "")
        }
        return book